package com.jiusit.common;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jiusit.dao.BankDao;
import com.webbuilder.utils.Base64Util;
import com.webbuilder.utils.DateUtil;
import com.webbuilder.utils.DbUtil;
import com.webbuilder.utils.JsonUtil;
import com.webbuilder.utils.MD5FileUtil;
import com.webbuilder.utils.SysUtil;
import com.webbuilder.utils.WebUtil;

public class BankServlet extends HttpServlet {
	private static final  Logger log  =  LoggerFactory.getLogger(BankServlet.class);
	private static final long serialVersionUID = 3421005242460829369L;
	
	public void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		log.info("BankServlet respContent:-----> USERNAME："+request.getParameter("USERNAME"));
		log.info("BankServlet respContent:-----> PASSWORD："+request.getParameter("PASSWORD"));
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		try {
			/*try{
				String USERNAME=request.getParameter("USERNAME");
				String PASSWORD=request.getParameter("PASSWORD");
				response.getWriter().write(USERNAME+","+PASSWORD);
			}catch(IOException e){
			    e.printStackTrace();
			}*/
			try{
				String USERNAME=request.getParameter("USERNAME");
				String PASSWORD=request.getParameter("PASSWORD");
				String PWD=MD5(PASSWORD);
				//验证账号密码
				String validation = "0";//验证通过:0,登入名或账号不正确:1,密码不正确:2
				String checkSql2 = "select key_name from wb_key where key_pid = '23ZSLWXOFP57' and key_is_leaf=1 and key_value = '"+USERNAME+"'";
		        ResultSet checkRs2 = DbUtil.query(request, checkSql2);
		        if(checkRs2.next()){
		        	USERNAME = checkRs2.getString(1);
		        }
		        System.out.println(checkSql2);
		        String checkSql = "select * from bank_users where user_name = '"+USERNAME+"'";
		        ResultSet checkRs = DbUtil.query(request, checkSql);
		        if(!checkRs.next()){
		        	validation = "1";
		        }
		        System.out.println(checkSql);
		        String id="";
		        String user_name="";
		        String open_bank_name="";
		        String currency="";
		        String currency_type="";
		        String user_name_account="";
		        String user_type_account="";
		        String start_date="";
		        String end_date="";
		        BankDao bankDao=new BankDao();
		        if(validation=="0"){
		        	String checkSql1 = "select t.*,t1.currency_name,t2.currency_type_name,IFNULL((select key_value user_name_account from wb_key where key_pid = '23ZSLWXOFP57' and key_is_leaf=1 and key_name like '%"+USERNAME+
		        			"%'),t.user_name) user_name_account,t4.user_type_account from bank_users t left join"+
		        " (select key_name,key_value currency_name from wb_key where key_pid = '23ZQZLVQL032' and key_is_leaf=1) t1 on t.currency=t1.key_name left join"+
		        " (select key_name,key_value user_type_account from wb_key where key_pid = '23ZQZLVQL01K' and key_is_leaf=1) t4 on t.user_type=t4.key_name left join"+
		        " (select key_name,key_value currency_type_name from wb_key where key_pid = '23ZQZLVQL05M' and key_is_leaf=1) t2 on t.currency_type=t2.key_name "+
		        			" where t.user_name = '"+USERNAME+"' and t.password = '"+PWD+"'";
		        	System.out.println(checkSql1);
		        	ResultSet checkRs1 = DbUtil.query(request, checkSql1);
		        	if(checkRs1.next()){
		        		id=checkRs1.getString(1);
		        		user_name=checkRs1.getString("user_name");
		        		open_bank_name=checkRs1.getString("open_bank_name");
		        		currency=checkRs1.getString("currency_name");
		        		currency_type=checkRs1.getString("currency_type_name");
		        		user_name_account=checkRs1.getString("user_name_account");
		        		user_type_account=checkRs1.getString("user_type_account");
		        		start_date=checkRs1.getString("start_date");
		        		end_date=checkRs1.getString("end_date");
		        		bankDao.setId(checkRs1.getString("id"));
		        		bankDao.setUser_name(checkRs1.getString("user_name"));
		        		bankDao.setOpen_bank_name(checkRs1.getString("open_bank_name"));
		        		bankDao.setCurrency(checkRs1.getString("currency_name"));
		        		bankDao.setCurrency_type(checkRs1.getString("currency_type_name"));
		        		bankDao.setUser_name_account(checkRs1.getString("user_name_account"));
		        		bankDao.setUser_type_account(checkRs1.getString("user_type_account"));
		        		bankDao.setStart_date(checkRs1.getString("start_date"));
		        		bankDao.setEnd_date(checkRs1.getString("end_date"));
		        	}else{
		        		if(validation=="1")validation="12";
		        		if(validation=="0")validation="2";
		        	}
		        }
		        
		        System.out.println("validation:"+validation);
		        HttpSession session = request.getSession();		
				session.setAttribute("ID",id);
				session.setAttribute("user_name",user_name);
				session.setAttribute("open_bank_name",open_bank_name);
				session.setAttribute("currency",currency);
				session.setAttribute("currency_type",currency_type);
				session.setAttribute("user_name_account",user_name_account);
				session.setAttribute("user_type_account",user_type_account);
				session.setAttribute("start_date",start_date);
				session.setAttribute("end_date",end_date);
				session.setAttribute("bankDao", bankDao);
				PrintWriter out = response.getWriter();
				out.write(validation);
				out.flush();
				out.close();
			}catch(IOException e){
			    e.printStackTrace();
			}
			/*
			response.setHeader("Cache-Control", "no-cache");
			PrintWriter out = response.getWriter();
			System.out.println(USERNAME);
			System.out.println(PASSWORD);
			out.write(USERNAME);
			out.flush();
			out.close();*/
			//request.getRequestDispatcher("/bank/login.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
     * 字符串MD5加密
     * @param s
     * @return
     */
    public final static String MD5(String s) {
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};       
        try {
            byte[] btInput = s.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
